package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.param.WmsOutStorageDto;
import com.macro.mall.wms.dto.WmsOutStorageItemDTO;
import com.macro.mall.model.WmsOutStorage;
import com.macro.mall.service.UmsAdminService;
import com.macro.mall.wms.dto.OutStorageDto;
import com.macro.mall.wms.service.WmsOutStorageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台商品出库管理
 *
 * @author 黄川
 * @Date 2020/9/10 16:25
 */
@Controller
@Api(tags = "WmsOutStorageController", description = "商品出库管理")
@RequestMapping("/outStorage")
public class WmsOutStorageController {

    @Autowired
    private WmsOutStorageService wmsOutStorageService;
    @Autowired
    private UmsAdminService adminService;


    /**
     * 查询门店所有出库单
     *
     * @param wmsOutStorage 出库单对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET )
    public CommonResult<CommonPage<WmsOutStorageDto>> getList(@CurrentUserName String currentUserName, WmsOutStorage wmsOutStorage,
                                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        UmsAdminDTO adminDTOByUserName = this.adminService.getAdminDTOByUserName(currentUserName);
        List<WmsOutStorageDto> list = wmsOutStorageService.getList(adminDTOByUserName.getStoreId(),wmsOutStorage, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
    /**
     * 根据出库单id获取出库单及子出库单信息
     *
     * @param orderId 出库单对象id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOutStorageItems", method = RequestMethod.GET )
    public CommonResult<List<WmsOutStorageItemDTO>> getOutStorageItems(@RequestParam Long  orderId) {
        List<WmsOutStorageItemDTO> list = wmsOutStorageService.getOutStorageItems(orderId);
        return CommonResult.success(list);
    }
    /**
     * 根据id查询出库单详情
     *
     * @param id 出库单id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOutStorage", method = RequestMethod.GET)
    public CommonResult getOutStorage(@RequestParam Long id) {
        return wmsOutStorageService.getOutStorage(id);
    }

    /**
     * 新增出库单
     *
     * @param currentUserName admin账号信息
     * @param outStorageDto            出库商品单list集合
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@CurrentUserName String currentUserName,
                               @RequestBody OutStorageDto outStorageDto) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return wmsOutStorageService.create(admin.getStoreId(), admin.getId(), admin.getUserName(), outStorageDto);
//        return CommonResult.failed();
    }

    /**
     * 未审核修改出库单
     *
     * @param id              出库单id
     * @param currentUserName admin账号信息
     * @param outStorageDto            出库商品单list集合
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/reviewed/{id}", method = RequestMethod.POST)
    public CommonResult reviewed(@PathVariable(value = "id") Long id, @CurrentUserName String currentUserName,
                                 @RequestBody OutStorageDto outStorageDto) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return wmsOutStorageService.reviewed(id, admin.getStoreId(), admin.getId(), admin.getUserName(), outStorageDto);
    }

    /**
     * 审核出库单，修改库存
     *
     * @param id              出库单id
     * @param currentUserName admin账号信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestParam Long id, @CurrentUserName String currentUserName) {
        CommonResult result = null;
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        try {
            result = wmsOutStorageService.update(id, admin.getStoreId(), admin.getId(), admin.getUserName(),admin.getAdminType());
        } catch (RuntimeException e) {
            result =  CommonResult.failed(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除未审核的出库单
     *
     * @param id              出库单id
     * @param currentUserName admin账号信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestParam(value = "id") Long id, @CurrentUserName String currentUserName) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUserName);
        return wmsOutStorageService.delete(id, admin.getId());
    }
}
